/**
 * File Name   : FinancialAction.java
 * Description : 
 */
package com.hh.admin.model.financial.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.common.service.CommonService;
import com.hh.admin.model.financial.form.FinancialForm;
import com.hh.admin.model.financial.form.ParamFinancialBean;
import com.hh.admin.model.financial.form.ResultFinancialBean;
import com.hh.admin.model.financial.service.FinancialService;
import com.hh.common.bean.PortalLocalBean;
import com.hh.common.consts.AdminConst;

/**
 * 理财课堂Action
 * @author 冯昌硕
 * @create 2013-1-21 下午10:52:00
 */
public class FinancialAction extends MappingDispatchAction {
    
    private static final Logger log = Logger.getLogger(FinancialAction.class);

    /**
     * 公共Service
     */
    private CommonService commonService;
    
    private FinancialService financialService;
    
    /**
     * 获取理财课堂内容列表
     * @create 2013-1-22 下午8:45:03
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FinancialForm fForm = (FinancialForm) form;
        
        // 查询当前页要显示的记录
        ParamFinancialBean paramBean = new ParamFinancialBean();
        List<ResultFinancialBean> financialLst = 
                commonService.selRecordList(fForm, "financial.selFinancialList", paramBean);
        request.setAttribute("financialLst", financialLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(paramBean, "financial.selFinancialCount");
        fForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / fForm.getPageSize();
        if(totleCnt % fForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        fForm.setTotlePage(pageCnt);
        log.debug("查询理财课堂记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 添加记录
     * @create 2013-1-22 下午9:25:32
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FinancialForm fForm = (FinancialForm) form;
        
        LoginUserInfo loginUserInfo = 
                (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        fForm.setCreateUser(loginUserInfo.getLoginName());
        
        int newId = financialService.insertFinancial(fForm);
        log.debug("新增理财课堂内容ID=[" + newId + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * 删除单条记录
     * @create 2013-1-22 下午9:54:38
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FinancialForm niForm = (FinancialForm) form;
        
        int id = niForm.getId();
        log.debug("理财课堂内容删除，要删除的记录ID=[" + id + "]");
        int delCnt = financialService.deleteOneFinancial(id);
        log.debug("理财课堂[" + id + "]删除，影响行数=[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 批量删除
     * @create 2013-1-22 下午9:56:27
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FinancialForm niForm = (FinancialForm) form;
        Integer[] ids = niForm.getIds();
        
        int delCnt = financialService.deleteManyFinancial(ids);
        log.debug("批量理财课堂内容删除，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 打开内容编辑页面
     * @create 2013-1-22 下午10:17:36
     */
    public ActionForward detailForEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FinancialForm niForm = (FinancialForm) form;
        int id = niForm.getId();
        ResultFinancialBean result = financialService.getFinancialDetail(id);
        
        request.setAttribute("financialDetail", result);
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑后的内容更新
     * @create 2013-1-22 下午10:17:46
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FinancialForm niForm = (FinancialForm) form;
        int updCnt = financialService.updateFinancial(niForm);
        log.debug("修改理财课堂[" + niForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * Portal页面查询使用，获取要在首页显示的理财课堂内容记录
     * @create 2013-2-2 下午9:30:53
     */
    public ActionForward portalLst(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FinancialForm fForm = (FinancialForm) form;
        
        // 查询当前页要显示的记录
        ParamFinancialBean paramBean = new ParamFinancialBean();
        List<ResultFinancialBean> financialLst = 
                commonService.selRecordList(fForm, "financial.selPortalFinancialList", paramBean);
        request.setAttribute("financialLst", financialLst);
        return mapping.findForward("portalLst");
    }
    
    /**
     * 导航菜单：理财课堂列表查询
     * @create 2013-2-16 下午8:47:15
     */
    public ActionForward financialLst(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 设置当前位置路径
        PortalLocalBean firLocalBean = new PortalLocalBean();
        firLocalBean.setShowName("理财课堂");
        firLocalBean.setHref("./financialLst.do");
        request.setAttribute("firLocalBean", firLocalBean);
        
        FinancialForm fForm = (FinancialForm) form;
        // 查询当前页要显示的记录
        ParamFinancialBean paramBean = new ParamFinancialBean();
        List<ResultFinancialBean> financialLst = 
                commonService.selRecordList(fForm, "financial.selPortalFinancialList", paramBean);
        request.setAttribute("financialLst", financialLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(paramBean, "financial.selPortalFinancialCount");
        fForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / fForm.getPageSize();
        if(totleCnt % fForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        fForm.setTotlePage(pageCnt);
        log.debug("查询理财课堂记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("navLst");
    }
    
    /**
     * 打开内容详情页面
     * @create 2013-1-22 下午10:26:36
     */
    public ActionForward portalDetail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        // 设置当前位置路径
        PortalLocalBean firLocalBean = new PortalLocalBean();
        firLocalBean.setShowName("理财课堂");
        firLocalBean.setHref("./financialLst.do");
        request.setAttribute("firLocalBean", firLocalBean);
        
        FinancialForm niForm = (FinancialForm) form;
        int id = niForm.getId();
        ResultFinancialBean result = financialService.getFinancialDetail(id);
        
        request.setAttribute("financialDetail", result);
        return mapping.findForward("detail");
    }

    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }


    public void setFinancialService(FinancialService financialService) {
        this.financialService = financialService;
    }
    
}
